package com.ymatou.shop.util;

import java.lang.reflect.Method;


import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Rect;
import android.location.LocationManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.provider.Settings.Secure;

public class DeviceUtil {
	
	public static final int SDK_VERSION_1_5 = 3;

	public static final int SDK_VERSION_1_6 = 4;
	
	public static final int SDK_VERSION_2_0 = 5;
	
	public static final int SDK_VERSION_2_0_1 = 6;
	
	public static final int SDK_VERSION_2_1 = 7;
	
	public static final int SDK_VERSION_2_2 = 8;
	
	public static final int SDK_VERSION_2_3 = 9;
	
	public static final int SDK_VERSION_2_3_3 = 10;
	
	public static final int SDK_VERSION_3_0 = 11;
	
	/**
	 * 获得应用类型 android, ios
	 * @return android
	 */
	public static String getDeviceType() {
        return "android";
    }
	
	/**
	 * 获得设备型号
	 * @return
	 */
	public static String getDeviceModel() {
        return StringUtil.makeSafe(Build.MODEL);
    }
	
	/**
	 * 获得设备制造商
	 * @return
	 */
	public static String getManufacturer() {
		return StringUtil.makeSafe(Build.MANUFACTURER);
	}
	
	
	
	/**
	 * 获得设备的固件版本号
	 */
	public static String getReleaseVersion() {
		return StringUtil.makeSafe(Build.VERSION.RELEASE);
	}
	
	/**
	 * 获得国际移动设备身份码
	 * @param context
	 * @return
	 */
	public static String getIMEI(Context context) {
        return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }
	
	/**
	 * 获得国际移动用户识别码
	 * @param context
	 * @return
	 */
	public static String getIMSI(Context context) {
        return ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId();
    }
	
	/**
	 * 获得设备屏幕矩形区域范围
	 * @param context
	 * @return
	 */
	public static Rect getScreenRect(Context context) {
		if (context == null){
			return new Rect(0, 0, 0, 0);
		}
        Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int w = display.getWidth();
        int h = display.getHeight();
        return new Rect(0, 0, w, h);
    }
	
	public static int getScreenHeight(Context context) {
			if (context == null){
				return 800;
			}
        Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int h = display.getHeight();
        return h;
    }
	
	public static int getScreenWidth(Context context) {
		if (context == null){
			return 0;
		}
        Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int w = display.getWidth();
        return w;
    }
	
	/**
	 * 获得设备屏幕密度
	 */
	public static float getScreenDensity(Context context) {
		DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
		return metrics.density;
	}
	
	public static int getScreenDensityDpi(Context context) {
		DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
		return (int)(metrics.density * 160);
	}
	
	/** 
	 * 获得系统版本
	 */
	public static String getSDKVersion(){
		return android.os.Build.VERSION.SDK;
	}
	
	public static int getSDKVersionInt(){
		try {
			return Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (Exception e) {
			return 0;
		}
		//return android.os.Build.VERSION.SDK_INT;
	}
	
	
	/**
	 * 获得android_id
	 * @param context
	 * @return
	 */
	public static String getAndroidId(Context context) {
		return Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
	}
		
	/**
	 * 获得deviceId
	 * @param context
	 * @return
	 */
	public static String getDeviceId(Context context) {
		return getIMEI(context);
	}
	
	/**
	 * 获得屏幕尺寸
	 * @param context
	 * @return
	 */
	public static String getResolution(Context context) {
		Rect rect = getScreenRect(context);
		return rect.right + "x" + rect.bottom;
	}
	
	public static String getSerialNumber() {
		String serialNumber = "";
		
		try {
			Class<?> c = Class.forName("android.os.SystemProperties");
			Method get = c.getMethod("get", String.class);
			serialNumber = (String) get.invoke(c, "ro.serialno");
			
			if(serialNumber.equals("")) {
				serialNumber = "?";
			}
		} catch (Exception e) {
			if(serialNumber.equals("")) {
				serialNumber = "?";
			}
		}
		
		return serialNumber;
	}
	
	public static PackageInfo getPackageInfo(Context context){
		try {
			return context.getPackageManager().getPackageInfo(context.getClass().getPackage().getName(), PackageManager.GET_CONFIGURATIONS);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean isOpenGPS(Context context) {
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		
		if (locationManager != null
				&& locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
			return true;
			
		return false;
	}
}
